package com.buaa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buaa.common.Result;
import com.buaa.dto.MapStoreDTO;
import com.buaa.dto.MapUpdateDTO;
import com.buaa.entity.Maps;
import com.buaa.service.IMapsService;
import com.buaa.vo.maps.MapsVO;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 地图数据表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2024-06-12
 */
@RestController
@RequestMapping("/maps")
@RequiredArgsConstructor
public class MapsController {

    private final IMapsService mapsService;

    @GetMapping()
    public Result<List<MapsVO>> getMapByPid(@RequestParam("pid") Integer pid) {
        return Result.success(mapsService.getMapsByPid(pid));
    }

    @GetMapping("getMap")
    public Result<Maps> getMapByCode(@RequestParam("adcode") Integer adcode) {
        QueryWrapper<Maps> queryWrapper = new QueryWrapper<>();
        return Result.success(mapsService.getOne(queryWrapper.eq("adcode", adcode), false));
    }

    @PostMapping("store")
    public Result<String> store(@RequestBody @Validated MapStoreDTO mapStoreDTO) {
        try {
            mapsService.store(mapStoreDTO);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
        return Result.success();
    }

    @PostMapping("update")
    public Result<String> update(@RequestBody @Valid MapUpdateDTO mapUpdateDTO) {
        try {
            mapsService.updateMaps(mapUpdateDTO);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.success();
    }
}
